<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
  <meta name="generator" content=
  "HTML Tidy for Linux/x86 (vers 1 September 2005), see www.w3.org">

  <title>fill_n</title>
  <link href="../cppreference.css" rel="stylesheet" type="text/css">
</head>

<body>
<table>
  <tr>
  <td>
  <div class="body-content">

  <div class="header-box">
    <a href="../index.html">cppreference.com</a> &gt; <a href=
    "index.html">C++ Algorithms</a> &gt; <a href=
    "fill_n.html">fill_n</a>
  </div>

  <div class="name-format">
    fill_n
  </div>

  <div class="syntax-name-format">
    Syntax:
  </div>
  <pre class="syntax-box">
  #include &lt;algorithm&gt;
  #include &lt;algorithm&gt;
  iterator fill_n( iterator start, size_t n, const <a href=
"../containers.html">TYPE</a>&amp; val );
</pre>

  <p>The fill_n() function is similar to (C++ I/O) <a href=
  "../cppio/fill.html">fill</a>(). Instead of assigning <em>val</em> to
  a range of elements, however, fill_n() assigns <em>val</em> to the
  first <em>n</em> elements starting at <em>start</em>.</p>

  <p>For example, the following code uses fill_n() to assign -1 to the
  first half of a vector of integers:</p>
  <pre class="example-code">
 vector&lt;int&gt; v1;
 for( int i = 0; i &lt; 10; i++ ) {
   v1.push_back( i );
 }              

 cout &lt;&lt; &quot;Before, v1 is: &quot;;
 for( unsigned int i = 0; i &lt; v1.size(); i++ ) {
   cout &lt;&lt; v1[i] &lt;&lt; &quot; &quot;;
 }
 cout &lt;&lt; endl;            

 fill_n( v1.begin(), v1.size()/2, -1 );         

 cout &lt;&lt; &quot;After, v1 is: &quot;;
 for( unsigned int i = 0; i &lt; v1.size(); i++ ) {
   cout &lt;&lt; v1[i] &lt;&lt; &quot; &quot;;
 }
 cout &lt;&lt; endl;            
</pre>

  <p>When run, this code displays:</p>
  <pre class="example-code">
 Before, v1 is: 0 1 2 3 4 5 6 7 8 9
 After, v1 is: -1 -1 -1 -1 -1 5 6 7 8 9         
</pre>

  <div class="related-name-format">
    Related topics:
  </div>

  <div class="related-content">
    <a href="fill.html">fill</a>
  </div>
  </div>
  </td>
  


  </tr>
  </table>
</body></html>
